package com.dyh.quickdemo.rulev2;

import com.alibaba.fastjson.JSON;
import com.dyh.quickdemo.GoodsRules.Goods;
import org.assertj.core.util.Lists;
import org.junit.jupiter.api.Test;
import org.springframework.util.StringUtils;

import java.util.List;

import static com.dyh.quickdemo.rulev2.Rules.define;
import static com.dyh.quickdemo.rulev2.Rules.not;

/*
@Author=Joey Wheeler
@Date=2021/3/8 13:44
*/
public class Mtest {

    @Test
    public void test(){
        List<Rule<Goods>> rules = Lists.newArrayList(
               define(Goods::getTitle,not(StringUtils::isEmpty),"标题不允许为空"),
               define(Goods::getAutoSupplement,e->e>0,"上架状态商品补货数量必须>0")
                       .depend(define(Goods::isOnSale))
        );

        Goods goods = new Goods();
        goods.setTitle("adw");
//        goods.setOnSale(true);
        goods.setAutoSupplement(-11);

        CheckResult checkResult=check(goods,rules);
        System.out.println(JSON.toJSONString(checkResult));

    }
    
    public static CheckResult check(Goods goods,List<Rule<Goods>> rules){
        CheckResult checkResult = null;
        for (Rule<Goods> rule : rules) {
            checkResult = rule.checkDetail(goods);
            if (!checkResult.isPass())
                return checkResult;

        }
        return checkResult;
    }

}
